package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.dto.EventPlan;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventJf;
import com.ruoyi.event.domain.bo.EventJfBo;
import com.ruoyi.event.domain.vo.EventJfVo;
import com.ruoyi.event.mapper.EventJfMapper;
import com.ruoyi.event.service.IEventJfService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 家访记录Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventJfServiceImpl implements IEventJfService {

    private final EventJfMapper baseMapper;

    /**
     * 查询家访记录
     */
    @Override
    public EventJfVo queryById(Long jfId) {
        return baseMapper.selectVoById(jfId);
    }

    /**
     * 查询家访记录列表
     */
    @Override
    public TableDataInfo<EventJfVo> queryPageList(EventJfBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventJf> lqw = buildQueryWrapper(bo);
        Page<EventJfVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询家访记录列表
     */
    @Override
    public List<EventJfVo> queryList(EventJfBo bo) {
        LambdaQueryWrapper<EventJf> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventJf> buildQueryWrapper(EventJfBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventJf> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventJf::getUserId, bo.getUserId());
        lqw.eq(bo.getMemberId() != null, EventJf::getMemberId, bo.getMemberId());
        lqw.eq(bo.getJfrq() != null, EventJf::getJfrq, bo.getJfrq());
        lqw.eq(StringUtils.isNotBlank(bo.getJfdd()), EventJf::getJfdd, bo.getJfdd());
        lqw.eq(StringUtils.isNotBlank(bo.getJfnr()), EventJf::getJfnr, bo.getJfnr());
        lqw.eq(StringUtils.isNotBlank(bo.getImg()), EventJf::getImg, bo.getImg());
        lqw.eq(StringUtils.isNotBlank(bo.getFj()), EventJf::getFj, bo.getFj());
        return lqw;
    }

    /**
     * 新增家访记录
     */
    @Override
    public Boolean insertByBo(EventJfBo bo) {
        EventJf add = BeanUtil.toBean(bo, EventJf.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setJfId(add.getJfId());
        }
        return flag;
    }

    /**
     * 修改家访记录
     */
    @Override
    public Boolean updateByBo(EventJfBo bo) {
        EventJf update = BeanUtil.toBean(bo, EventJf.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventJf entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除家访记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void renderEventInfo(Long userId, List<EventPlan> eventPlanList) {
        LambdaQueryWrapper<EventJf> lqw = Wrappers.lambdaQuery();
        lqw.eq(EventJf::getUserId, userId);
        lqw.orderByAsc(EventJf::getJfrq);
        List<EventJf> eventJfList = baseMapper.selectList(lqw);
        for (EventPlan eventPlan : eventPlanList) {
            for (EventJf eventJf : eventJfList) {
                if (eventPlan.isOccurInThisPlanCycle(eventJf.getJfrq())) {
                    eventPlan.setEventId(eventJf.getJfId());
                    eventPlan.setEventDate(eventJf.getJfrq());
                }
            }
        }
    }
}
